package bhbt.service.log;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.model.log.LogEle;
import bhbt.model.log.LogMoney;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.core.BaseService;
import bhbt.utils.ContextUtil;






@Service
public class LogEleService extends BaseService {
	@Resource
    private MemberService memberService;
	@Resource
    private LogMoneyService logMoneyService;
	@Resource
    private LogEleService logEleService;
	@Resource
    private ParamWageService paramWageService;
	@Resource
    private DailiService dailiService;
	
	public void saveZhuan(Double ele){
		 
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		
		//根据市场价格算出转换的现金币
		ParamWage paramWage1000 = this.paramWageService.getByKey(1000);
		
		Boolean result = false;
		//看看会员是否能按照股市价兑换
		Daili daili = this.dailiService.getByKey(member.getDailiId());
		if(daili.getId()==10){
			ParamWage paramWage1200=paramWageService.getByKey(1200);
    		ParamWage paramWage1201=paramWageService.getByKey(1201);
    		if(member.getRecommendYe()>=paramWage1200.getValue()&&member.getRecommendTotalYe()>=paramWage1201.getValue()){
    			result=true;
    		}
    		 
		}else if(daili.getId()==20){
			
    		ParamWage paramWage1202=paramWageService.getByKey(1202);
    		ParamWage paramWage1203=paramWageService.getByKey(1203);
    		if(member.getRecommendYe()>=paramWage1202.getValue()&&member.getRecommendTotalYe()>=paramWage1203.getValue()){
    			result=true;
    		}
		}else if(daili.getId()==30){
			
    		ParamWage paramWage1204=paramWageService.getByKey(1204);
    		ParamWage paramWage1205=paramWageService.getByKey(1205);
    		if(member.getRecommendYe()>=paramWage1204.getValue()&&member.getRecommendTotalYe()>=paramWage1205.getValue()){
    			result=true;
    		}
		}
		
		
		/**********************业务操作***************************/
		if(member.getEle()>=ele){
			Double money = 0d;
			if(result){
				money = ele*paramWage1000.getValue();
			}else{
				money = ele;
			}
			
			
			
			Map<String, Object> mapMember = new HashMap<String, Object>();
			mapMember.put("id",member.getId());
			mapMember.put("diyUpdate","money=money+"+money+",ele=ele-"+ele);
	        //更新余额 
	        memberService.update(mapMember);
	        member= memberService.getByKey(member.getId());
	        Double moneyYu = member.getMoney();
	        
	        //日志
	        LogMoney logMoney = new LogMoney();
	        logMoney.setMemberId(member.getId());
	        if(result){
	        	logMoney.setTitle("原石转换，转换数："+ele+"，当时原石价格："+paramWage1000.getValue());
	        }else{
	        	logMoney.setTitle("原石转换，转换数："+ele+"，您没达到条件，只能按1:1兑换。");
	        }
	        
	        logMoney.setState(88);
	        logMoney.setTotalNum(money);
	        logMoney.setNum(money);
	        logMoney.setYu(moneyYu);
	        logMoney.setDongNum(0d);
	        logMoney.setFaDate(new Date());
	        logMoneyService.saveOrUpdate(logMoney);
	        
	        
	        
	        Double eleYu = member.getMoney();
	        
	        //日志
	        LogEle logEle = new LogEle();
	        logEle.setMemberId(member.getId());
	        if(result){
	        	logEle.setTitle("原石转换，转换数："+ele+"，当时原石价格："+paramWage1000.getValue());
	        }else{
	        	logEle.setTitle("原石转换，转换数："+ele+"，您没达到条件，只能按1:1兑换。");
	        }
	        
	        
	        logEle.setState(88);
	         
	        logEle.setNum(ele);
	        logEle.setYu(eleYu);
	       
	        logEleService.saveOrUpdate(logEle);
	        
		}
		
		

         
       
        
      
         
		
	}

}
